<!DOCTYPE html>
<html lang="en">

	<head>
		<meta charset="UTF-8">
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
		<title>注册页</title>
		<link rel="stylesheet" href="css/layui.css">
		<link rel="stylesheet" href="css/style.css">
		<script type="text/javascript" src="js/layui.all.js"></script>

	</head>

	<body>
		<form class="layui-form" action="" id="form">
			<div class="layui-form-item">
				<label class="layui-form-label">商品名称</label>
				<div class="layui-input-inline">
					<input type="text" name="goodname" required lay-verify="required" placeholder="请输入商品名称" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">商品价格</label>
				<div class="layui-input-inline">
					<input type="" name="price" required lay-verify="required" onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}" onafterpaste="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}" placeholder="商品价格(只能输入正整数)" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">商品图片</label>
				<div class="layui-input-inline">
					<button type="button" class="layui-btn" id="test1">
			
  <i class="layui-icon">&#xe67c;</i>上传图片
</button>
					<button type="button" class="layui-btn" id="btn">上传</button>

				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-input-block">
					<input class="layui-btn" type="button" value="立即提交" id="goods" />
					<button type="reset" class="layui-btn layui-btn-primary">重置</button>
				</div>
			</div>
		</form>

		<script type="text/javascript">
			function getUrl(url) {
				return "http://localhost:8080/api/v1/" + url;
			}

			layui.use(['upload', 'form', 'layer'], function() {

				var upload = layui.upload;
				var $ = layui.jquery;
				var form = layui.form;
				var layer = layui.layer;
				//执行实例
				var uploadInst = upload.render({
					elem: '#test1' //绑定元素
						,
					url: getUrl("pictureInsert") //上传接口
						,
					method: "post",
					multiple: true,
					bindAction: '#btn',
					auto: false,
					response: {
						statusName: 'code' //规定数据状态的字段名称，默认：code
							,
						statusCode: 1000 //规定成功的状态码，默认：0
							,
						msgName: 'mes' //规定状态信息的字段名称，默认：msg
							,
						countName: 'total' //规定数据总数的字段名称，默认：count
							,
						dataName: 'data' //规定数据列表的字段名称，默认：data
					},
					field: "file",
					number: 5,
					//					auto: false,
					done: function(res, index, upload,obj) {
						//上传完毕回调
//						alert(result.src);
						alert(res.imgurl)
						$("#form").append($("<input />").attr("type", "hidden").val(res.imgurl).attr("name", "filename"));

					},
					error: function() {
						//请求异常回调
					}
				});
				$("#goods").click(function(){
					var fd = new FormData(document.getElementById("form"));
						$.ajax({
							type: "post",
							url: getUrl("goodsInsert"),
							async: true,
							data: fd,
							processData: false,
							contentType: false,
							success: function(data) {
								if(data == true) {
									alert('添加成功');
									window.location.href='goods.html'

								} else {
									alert('添加失败');
									window.location.href='addGood.html'

								}
							}
						});
						
				})

			});

			//Demo

			//监听提交
		</script>

	</body>

</html>